// 解码方法

package Leetcode;

class solution_091 {
    public int numDecodings(String s) {
        int length = s.length();
        int[] dp = new int[length + 1];
        dp[0] = 1;
        dp[1] = s.charAt(0) == '0' ? 0 : 1;
        for (int i = 2; i <= length; i++) {
            char before = s.charAt(i - 2);
            char current = s.charAt(i - 1);
            // 如果before='0'，dp[i]=dp[i-1]
            if (before == '0') {
                if (current == '0') {
                    dp[i] = 0;
                    continue;
                } else {
                    dp[i] = dp[i - 1];
                    continue;
                }
            } else {
                int num = 10 * (before - '0') + (current - '0');
                if (num >= 1 && num <= 26)
                    dp[i] += dp[i - 2];
                if (current != '0')
                    dp[i] += dp[i - 1];
            } // else before !='0'
        } // for
        return dp[length];
    }
}

public class Solution091 {

}
